#include "sprite.h"

Tien::Tien(LPD3DXSPRITE SpriteHandler, char *Path, int Width, int Height, int Count, int SpritePerRow) 
{
	D3DXIMAGE_INFO info; 
	HRESULT result; 
	
	_Image = NULL;
	_SpriteHandler = SpriteHandler;

	_Width = Width;
	_Height = Height;
	_Count = Count;
	_SpritePerRow = SpritePerRow;
	_Index = 0;

	result = D3DXGetImageInfoFromFile(Path,&info);
	if (result!=D3D_OK)
	{
		int i = 10;	
	}

	//result = d3ddv->CreateOffscreenPlainSurface(
	//	info.Width,
	//	info.Height,
	//	D3DFMT_X8R8G8B8,
	//	D3DPOOL_DEFAULT,
	//	&_Surface,
	//	NULL);

	//if (result!=D3D_OK) 
	//{
	//	int i = 10;
	//}

	//result = 
	//	D3DXLoadSurfaceFromFile(
	//		_Surface,
	//		NULL,
	//		NULL,
	//		Path, 
	//		NULL,
	//		D3DX_DEFAULT,
	//		D3DCOLOR_XRGB(0,0,0),				// black is the transparent color
	//		NULL);

	LPDIRECT3DDEVICE9 d3ddv; 
	SpriteHandler->GetDevice(&d3ddv);

	D3DXCreateTextureFromFileEx(
		d3ddv,
		Path,
		info.Width,
		info.Height,
		1,
		D3DUSAGE_DYNAMIC,
		D3DFMT_UNKNOWN,
		D3DPOOL_DEFAULT,
		D3DX_DEFAULT,
		D3DX_DEFAULT,
		D3DCOLOR_XRGB(0,0,0),
		&info,
		NULL,
		&_Image);

	if (result!=D3D_OK) 
	{
		int i = 10;
	}
}


void Tien::Renders(int X, int Y)
{
	RECT srect;
	//RECT drect; 

	srect.left = (_Index % _SpritePerRow)*(_Width);
	srect.top = (_Index / _SpritePerRow)*(_Height);
	srect.right = srect.left + _Width -1 ;
	srect.bottom = srect.top + _Height - 1;

	//drect.left = X;
	//drect.right = X + _Width;
	//drect.top = Y;
	//drect.bottom = Y + _Height;

	//_SpriteHandler->Begin(D3DXSPRITE_ALPHABLEND);

	D3DXVECTOR3 position((float)X,(float)Y,0);

	_SpriteHandler->Draw(
		_Image,
		&srect,
		NULL,
		&position, 
		D3DCOLOR_XRGB(255,255,255)
	);


	//_SpriteHandler->End();

	//_DXDevice->StretchRect(_Surface,&srect,Target,&drect,D3DTEXF_NONE);

}

void Tien::Next() 
{
	_Index = (_Index + 1) % _Count;
}